<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/5/17
  Time: 11:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主机信息</title>
</head>
<body>
<div id="host_tb">
    <a class="easyui-linkbutton" onclick="addHost()" data-options="iconCls:'icon-add',plain:true">新增</a>
    <a class="easyui-linkbutton" onclick="deleteHost()" data-options="iconCls:'icon-remove',plain:true">删除</a>
    <a class="easyui-linkbutton" onclick="editHost()" data-options="iconCls:'icon-edit',plain:true">修改</a>
</div>
<table id="host_dg" class="easyui-datagrid" data-options="method:'post',fitColumns:true,fit:true,
                    ctrlSelect:true,scrollbarSize:0,url:'../host/findAllHostCustom',
                    pagination:true,toolbar:'#host_tb'">
    <thead>
    <tr>
        <th data-options="field:'id',width:9" rowspan="2">主机</th>
        <th colspan="3">CPU</th>
        <th colspan="4">内存</th>
        <th colspan="3">硬盘</th>
    </tr>
    <tr>
        <th data-options="field:'cpu_brand',width:9,formatter:function (value, row, index) {
                        return row.cpu.brand;
                    }">品牌</th>
        <th data-options="field:'cpu_model',width:9,formatter:function (value, row, index) {
                        return row.cpu.model;
                    }">型号</th>
        <th data-options="field:'cpu_freq',width:9,formatter:function (value, row, index) {
                        return row.cpu.frequency;
                    }">主频</th>
        <th data-options="field:'mem_brand',width:9,formatter:function (value, row, index) {
                        return row.memory.brand;
                    }">品牌</th>
        <th data-options="field:'mem_capa',width:9,formatter:function (value, row, index) {
                        return row.memory.capacity;
                    }">容量</th>
        <th data-options="field:'mem_freq',width:9,formatter:function (value, row, index) {
                        return row.memory.frequency;
                    }">频率</th>
        <th data-options="field:'mem_type',width:9,formatter:function (value, row, index) {
                        return row.memory.type;
                    }">传输类型</th>
        <th data-options="field:'disk_brand',width:9,formatter:function (value, row, index) {
                        return row.hardDisk.brand;
                    }">品牌</th>
        <th data-options="field:'disk_capa',width:9,formatter:function (value, row, index) {
                        return row.hardDisk.capacity;
                    }">容量</th>
        <th data-options="field:'disk_speed',width:9,formatter:function (value, row, index) {
                        return row.hardDisk.speed;
                    }">转速</th>
    </tr>
    </thead>
</table>
<script>
    $(function () {
        $('#host_dg').datagrid();
        $('#host_dg').datagrid('getPager').pagination({
            beforePageText:'第',
            afterPageText:'页，共{pages}页',
            displayMsg:'当前显示第{from}到{to}条，共{total}条'
        });
    });

    function addHost() {
        if ($('#tt').tabs('exists', '添加主机')) {
            $('#tt').tabs('select', '添加主机');
        } else {
            $('#tt').tabs('add', {
                title:'添加主机',
                closable:true,
                href:'../host/getAddHostView'
            });
        }
    }

    function deleteHost() {
        var row = $('#host_dg').datagrid('getSelected');
        if (row === null) {
            return;
        } else {
            $.messager.confirm({
                title: '确认',
                msg: '确定要删除吗？',
                ok:'确认',
                cancel:'取消',
                fn: function(r){
                    if (r){
                        var index = $('#host_dg').datagrid('getRowIndex', row);
                        $.ajax({
                            url:'../host/deleteHostById',
                            type:'post',
                            data:'id=' + row.id,
                            dataType:'json',
                            success:function (data) {
                                //var d = eval('(' + data + ')');
                                if (data.result === 'success') {
                                    $('#host_dg').datagrid('deleteRow', index);
                                }
                                $.messager.alert('提示', data.message, 'info');
                            }
                        });
                    }
                }
            });
        }
    }

    function editHost() {
        var row = $('#host_dg').datagrid('getSelected');
        if (row == null) {
            return;
        } else {
            if ($('#tt').tabs('exists', '修改主机')) {
                $('#tt').tabs('select', '修改主机');
                $.ajax({
                    url:'../host/findHostById',
                    type:'post',
                    data:'id=' + row.id,
                    dataType:'json',
                    success:function (data) {
                        //var d = eval('(' + data + ')');
                        $('#editHostForm').form('load', data);
                    }
                });
            } else {
                $('#tt').tabs('add', {
                    title:'修改主机',
                    closable:true,
                    href:'../host/getEditHostView'
                });
            }
        }
    }
</script>
</body>
</html>
